import { cloneElement } from 'react'
import * as R from 'ramda'

interface Attr {
    key: string
}

const withLayout = () => {
    let CurLayout: any = null
    let CurLayoutKey = ''
    return (Layout: any, attr: Attr, Component: any) => {
        if (attr.key !== CurLayoutKey) {
            CurLayoutKey = attr.key
            CurLayout = Layout
        }
        return cloneElement(<CurLayout />, undefined, <Component />)
    }
}

// 区分 component 和 element
// component：还未调用，或者还未被编译成 reactElement 后的执行结果 --> Layout
// element：被编译且执行后的结果 --> <Layout /> 如果是函数组件的话，这好比是调用函数一样

export default R.curry(withLayout())
